<template>
	<section>

		<el-col :span="24" class="toolbar" style="padding-bottom:50px;">
			<el-form :inline="true" :model="query">
				<el-form-item>
					<el-input v-model="query.keyword" placeholder="关键字"></el-input>
				</el-form-item>
        <el-form-item label="词典">
          <el-select v-model="query.type" value-key="id" placeholder="请选择">
            <el-option
                v-for="type in types"
                :key="type.id"
                :label="type.name"
                :value="type">
              <span style="float: left">{{ type.name }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ type.sn }}</span>
            </el-option>
          </el-select>
        </el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="search1">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd1">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

    <!--列表:优秀实践，copy domain修改列表-->
    <el-table :data="pageData.data" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="100">
      </el-table-column>
      <el-table-column type="index" width="100">
      </el-table-column>
      <el-table-column prop="name" label="词条名称" width="200" sortable>
      </el-table-column>
      <!--private Department department;//所属部门-->
      <el-table-column prop="type.id" label="类型编号" width="200" sortable>
      </el-table-column>
      <el-table-column label="操作" >
        <template scope="scope">
          <el-button size="small" @click="handleEdit1(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel1(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>



    <!--工具条-->
    <el-col :span="12" class="toolbar">
      <el-button type="danger" @click="batchRemove1" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange1" :current-page="query.currentPage" :page-size="query.pageSize" :total="pageData.total" style="float:right;">
      </el-pagination>
    </el-col>

		<!--编辑界面
		    v-model="editFormVisible" 老版本打开对话框
		-->
		<el-dialog :title="title" :visible.sync="detailVisible" :close-on-click-modal="false">
			<el-form :model="detail" label-width="80px" :rules="detailRules" ref="detailForm">
				<el-form-item label="明细" prop="name">
					<el-input v-model="detail.name" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="词典">
          <el-select v-model="detail.type" value-key="id" placeholder="请选择">
            <el-option
                v-for="type in types"
                :key="type.id"
                :label="type.name"
                :value="type">
              <span style="float: left">{{ type.name }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ type.sn }}</span>
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
			  <div slot="footer" class="dialog-footer">
				<el-button @click.native="typeVisible = false">取消</el-button>
				<el-button type="primary" @click.native="saveSubmit1" :loading="detailLoading">保存</el-button>
			  </div>
		</el-dialog>


	</section>
</template>

<script>
	export default {
		data() {
			return {
				pageData:{
          total:0,//分页条
					data:[]//数据表格
				},
				query:{
          keyword:'',
          currentPage:1,//分页条
					pageSize:5 ,//分页条
          type:{}//
				},
				listLoading: false,
				sels: [],//列表选中列
        title:'',
        detailVisible: false,//新增界面是否显示
        detailLoading: false,
        detailRules: {
          name: [
            { required: true, message: '请输入词条名称', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '请选择词条类型', trigger: 'blur' }
          ]
				},
				//新增界面数据
        detail: {
          id: null,
          name: '',
          type:{}
        },
        detailType: {
          id:null,
          name:'',
          sn:''
        },
        types:[]
			}
		},
		methods: {
			//状态显示转换-不能上色
            // stateFormatter: function (row, column) {
             //    if(row.state==0){
             //        return "正常"
			// 	}else{
             //        return "<font style='color: red;'>禁用</font>"
			// 	}
			// },
			search1(){
			    //修改页码
			    this.query.currentPage =1;
			    //改查询条件=双向绑定输入时自动改变，只需要重新查询一次就ok
			    this.getDetails();
			},
			handleCurrentChange1(val) {
				this.query.currentPage = val;
				this.getDetails();
			},
			//获取词条列表
			getDetails() {
				this.listLoading = true; //加载条
				this.$http.post("/detail",this.query)
                    .then(result => {
                        this.pageData = result.data;
                        this.listLoading = false; //去除加载条
                    })
					.catch(result => {
					    alert("获取词典失败");
					})
			},
      getTypes(){
        this.$http.get("/detailType")
          .then(result =>{
            this.types = result.data
          })
      },
      // getDetailType() {
      //   this.listLoading = true; //加载条
      //   this.$http.post("/detailType",this.query)
      //       .then(result => {
      //         this.pageData = result.data;
      //         this.listLoading = false; //去除加载条
      //       })
      //       .catch(result => {
      //         alert("获取词典失败");
      //       })
      // },

			//删除
			handleDel1: function (index, row) {
				this.$confirm('确认删除该记录吗?', '温馨提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					let url = "/detail/" + row.id
          this.$http.delete(url)
					 .then((result) => {
						this.listLoading = false;
            result = result.data
            if(result.success){
              this.$message({
                message: '删除成功',
                type: 'success'
              });
            }else{
              this.$message({
                message: result.message,
                type: 'error'
              });
            }
						this.getDetails();
					});
				}).catch(() => {
				});
			},
			//显示编辑界面
			handleEdit1: function (index, row) {
			  this.title="修改员工"
        this.getTypes();//获取部门数据
				this.detailVisible = true;//弹出修改框
				this.detail = Object.assign({}, row);//数据回显
      },
			//显示新增界面
			handleAdd1: function () {
			  this.title="添加部门"
        this.getTypes();//获取types数据
				this.detailVisible = true;
        this.detail = { //置空，以免先点编辑后点新增时，有缓存显示
          id: null,
          name: '',
          type:{}
        }
			},
			//保存
			saveSubmit1: function () {
				this.$refs.detailForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.detailLoading = true;
							let para = Object.assign({}, this.detail)
              this.$http.put("/detail",para)
							  .then((result) => {
							  	this.detailLoading = false;
							  	result = result.data
							  	if(result.success){
                     this.$message({
                     message: '提交成功',
                     type: 'success'
                     })
                     this.$refs['detailForm'].resetFields();//重置表单
                     this.detailVisible = false;//隐藏模态框
                     this.getDetails();//加载当前数据
                  }else{
                    this.$message({
                      message: result.msg,
                      type: 'error'
                      //保存成功 则提交表单 保存失败 可修改后重新提交
                    });
                  }
							});
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove1: function () {
        var ids = this.sels.map(item => item.id);//[34,27]
				this.$confirm('确认删除选中记录吗？', '温馨提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;

          this.$http.patch("/detail",ids).then((result) => {
						this.listLoading = false;
            result = result.data
            if(result.success){
              this.$message({
                message: '删除成功',
                type: 'success'
              })
            }else{
              this.$message({
                message: result.msg,
                type: 'error'
              })
            }
						this.handleCurrentChange1(1)
					});
				}).catch(() => {
				});
			}
		},
		mounted() {
			this.getDetails();
			this.getTypes();
		}
	}

</script>

<style scoped>

</style>